package com.tianqu.tms.www.rental.action;

import java.io.File;
import java.io.FileOutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.core.util.ImageUtils;
import com.tianqu.tms.core.util.Registry;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.www.rental.actionform.RentalContractImgInputForm;
import com.tianqu.tms.www.rental.viewhelper.RentalContractAddInputViewHelper;
import com.tianqu.tms.www.rental.viewhelper.RentalContractImgInputViewHelper;

/**
 * 图片上传
 * @author mufeiyu
 *
 */
public class RentalContractImageAddConfirmAction extends TmsAction {
	private Logger logger = Logger.getLogger(RentalContractImageAddConfirmAction.class);
	private static final String ITEMSPATH="ItemsPath";//获取项目路径
	// return
	public static String UPLOAD = "upload";
	public static String IMG1 = "1";
	public static String IMG2 = "2";
	public static String IMG3 = "3";
	public static String IMG4 = "4";
	public static String IMG5 = "5";
	public static String IMG6 = "6";
	public static String IMG7 = "7";
	public static String IMG8 = "8";
	private String sysPath =null;

	RentalContractImgInputViewHelper viewHelper = new RentalContractImgInputViewHelper();

	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		

		// 文件路径
		sysPath=(String) uc.getAttribute(ITEMSPATH);
		RentalContractImgInputForm ImForm = (RentalContractImgInputForm) form;
		if (ImForm.getButton1() != null) {
			// 返回
			RentalContractAddInputViewHelper viewH = (RentalContractAddInputViewHelper) uc.getAttribute(TmsConstants.VIEWHELP);
			request.setAttribute(TmsConstants.VIEWHELP, viewH);
			
		} else if (ImForm.getButton2() != null) {
			RentalContractImgInputForm Forms= (RentalContractImgInputForm) form;
			FormFile formfil = Forms.getImgPath();
			//判断图片是否正确
			if(formfil.getFileSize() > 1048576){
				setErrorsToRequest(request, TmsConstants.I0018 ,null);
				request.setAttribute(TmsConstants.VIEWHELP,viewHelper);
	            return mapping.findForward(UPLOAD);	
			}
			// 上传
			String ImageName = System.currentTimeMillis() + "." + getImgFormat(formfil);// 图片名称
			String images = ImagesUpLoad(form,ImageName);
			RentalContractImgInputViewHelper view = new RentalContractImgInputViewHelper();
			view.setImages(images);//页面图片名
			view.setImgPathidden(images);
			
			request.setAttribute(TmsConstants.VIEWHELP, view);
			return mapping.findForward(UPLOAD);
		} else if (ImForm.getButton3() != null) {
			// 确认
			RentalContractAddInputViewHelper viewH = (RentalContractAddInputViewHelper) uc
					.getAttribute(TmsConstants.VIEWHELP);
			RentalContractImgInputForm formpath = (RentalContractImgInputForm) form;

			String imagesPath = formpath.getImages();
			if (viewH.getButtonType().equals(IMG1)) {
				viewH.setPhoto_1(imagesPath);
			} else if (viewH.getButtonType().equals(IMG2)) {
				viewH.setPhoto_2(imagesPath);
			} else if (viewH.getButtonType().equals(IMG3)) {
				viewH.setPhoto_3(imagesPath);
			} else if (viewH.getButtonType().equals(IMG4)) {
				viewH.setPhoto_4(imagesPath);
			} else if (viewH.getButtonType().equals(IMG5)) {
				viewH.setPhoto_5(imagesPath);
			} else if (viewH.getButtonType().equals(IMG6)) {
				viewH.setPhoto_6(imagesPath);
			} else if (viewH.getButtonType().equals(IMG7)) {
				viewH.setPhoto_7(imagesPath);
			} else if (viewH.getButtonType().equals(IMG8)) {
				viewH.setPhoto_8(imagesPath);
			}
			viewH.setButtonType("");
			uc.setAttribute(TmsConstants.VIEWHELP, viewH);
			request.setAttribute(TmsConstants.VIEWHELP, viewH);
		}
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(TmsConstants.SUCCESS);
	}

	/**
	 * 图片上传到临时文件夹
	 * 
	 * @param form
	 */
	private String ImagesUpLoad(ActionForm form,String ImageName) {
		RentalContractImgInputForm ImForm = (RentalContractImgInputForm) form;
		FormFile formfil = ImForm.getImgPath();
		String path = ImageUtils.getImageDirectoryPath(Registry.getConfig(TmsConstants.UPLOAD_RENTAL_IMAGE_PATCH_TMP_KEY));
		try {
			@SuppressWarnings("resource")
			FileOutputStream fileOutput = new FileOutputStream(new File(path+ ImageName));
			fileOutput.write(formfil.getFileData());
			fileOutput.flush();
			return ImageUtils.getImageUrl(Registry.getConfig(TmsConstants.UPLOAD_RENTAL_IMAGE_PATCH_TMP_KEY)+ ImageName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 图片格式取得
	 */
	public String getImgFormat(FormFile file) {
		int i = 0;
		String type = file.getFileName();
		if(type.length()==0)return "";
		while (i != -1) {
			// 找到上传文件的类型的位置，这个地方的是'.'
			i = type.indexOf(".");
			/* 截取上传文件的后缀名,此时得到了文件的类型 */
			type = type.substring(i + 1).toLowerCase();
		}
		return type;
	}
}
